Using the Taken's Theorem we can embedd the time series we have available into a phase space where we do not have a temporal dependence between our data points anymore. This is important because the Statistical Learning Theory only guarantees the learning of an algorithm if the samples (rows or data instances) of our dataset are independent from each other.
In order to define this embedding, we need two hyperparameters named d and m which are the time delay and the embedding dimension respectively. The time delay defines how many data points, previously to the actual one, we will use on the embedding. The dimension will define the final shape of our dataset and how many axies there are in the phase space, each axis representing a time delay.
!pip install giotto-tda
!pip install openml
!pip install git+https://github.com/manu-mannattil/nolitsa.git
!pip install plotly
!pip install sktime --user
Requirement already satisfied: giotto-tda in c:\users\dell\anaconda3\lib\site-packages (0.5.1) Requirement already satisfied: pyflagser>=0.4.3 in c:\users\dell\anaconda3\lib\site-packages (from giotto-tda) (0.4.4) Requirement already satisfied: joblib>=0.16.0 in c:\users\dell\anaconda3\lib\site-packages (from giotto-tda) (1.1.0) Requirement already satisfied: ipywidgets>=7.5.1 in c:\users\dell\anaconda3\lib\site-packages (from giotto-tda) (7.6.5) Requirement already satisfied: numpy>=1.19.1 in c:\users\dell\anaconda3\lib\site-packages (from giotto-tda) (1.21.5) Requirement already satisfied: scikit-learn>=0.23.1 in c:\users\dell\anaconda3\lib\site-packages (from giotto-tda) (0.24.2) Requirement already satisfied: scipy>=1.5.0 in c:\users\dell\anaconda3\lib\site-packages (from giotto-tda) (1.7.1) Requirement already satisfied: python-igraph>=0.8.2 in c:\users\dell\anaconda3\lib\site-packages (from giotto-tda) (0.9.9) Requirement already satisfied: plotly>=4.8.2 in c:\users\dell\anaconda3\lib\site-packages (from giotto-tda) (5.6.0) Requirement already satisfied: ipython-genutils~=0.2.0 in c:\users\dell\anaconda3\lib\site-packages (from ipywidgets>=7.5.1->giotto-tda) (0.2.0) Requirement already satisfied: ipykernel>=4.5.1 in c:\users\dell\anaconda3\lib\site-packages (from ipywidgets>=7.5.1->giotto-tda) (6.4.1) Requirement already satisfied: ipython>=4.0.0 in c:\users\dell\anaconda3\lib\site-packages (from ipywidgets>=7.5.1->giotto-tda) (7.29.0) Requirement already satisfied: nbformat>=4.2.0 in c:\users\dell\anaconda3\lib\site-packages (from ipywidgets>=7.5.1->giotto-tda) (5.1.3) Requirement already satisfied: widgetsnbextension~=3.5.0 in c:\users\dell\anaconda3\lib\site-packages (from ipywidgets>=7.5.1->giotto-tda) (3.5.1) Requirement already satisfied: jupyterlab-widgets>=1.0.0 in c:\users\dell\anaconda3\lib\site-packages (from ipywidgets>=7.5.1->giotto-tda) (1.0.0) Requirement already satisfied: traitlets>=4.3.1 in c:\users\dell\anaconda3\lib\site-packages (from ipywidgets>=7.5.1->giotto-tda) (5.1.0) Requirement already satisfied: matplotlib-inline<0.2.0,>=0.1.0 in c:\users\dell\anaconda3\lib\site-packages (from ipykernel>=4.5.1->ipywidgets>=7.5.1->giotto-tda) (0.1.2) Requirement already satisfied: debugpy<2.0,>=1.0.0 in c:\users\dell\anaconda3\lib\site-packages (from ipykernel>=4.5.1->ipywidgets>=7.5.1->giotto-tda) (1.4.1) Requirement already satisfied: jupyter-client<8.0 in c:\users\dell\anaconda3\lib\site-packages (from ipykernel>=4.5.1->ipywidgets>=7.5.1->giotto-tda) (6.1.12) Requirement already satisfied: tornado<7.0,>=4.2 in c:\users\dell\anaconda3\lib\site-packages (from ipykernel>=4.5.1->ipywidgets>=7.5.1->giotto-tda) (6.1) Requirement already satisfied: backcall in c:\users\dell\anaconda3\lib\site-packages (from ipython>=4.0.0->ipywidgets>=7.5.1->giotto-tda) (0.2.0) Requirement already satisfied: jedi>=0.16 in c:\users\dell\anaconda3\lib\site-packages (from ipython>=4.0.0->ipywidgets>=7.5.1->giotto-tda) (0.18.0) Requirement already satisfied: pickleshare in c:\users\dell\anaconda3\lib\site-packages (from ipython>=4.0.0->ipywidgets>=7.5.1->giotto-tda) (0.7.5) Requirement already satisfied: colorama in c:\users\dell\anaconda3\lib\site-packages (from ipython>=4.0.0->ipywidgets>=7.5.1->giotto-tda) (0.4.4) Requirement already satisfied: setuptools>=18.5 in c:\users\dell\anaconda3\lib\site-packages (from ipython>=4.0.0->ipywidgets>=7.5.1->giotto-tda) (58.0.4) Requirement already satisfied: decorator in c:\users\dell\anaconda3\lib\site-packages (from ipython>=4.0.0->ipywidgets>=7.5.1->giotto-tda) (5.1.0) Requirement already satisfied: prompt-toolkit!=3.0.0,!=3.0.1,<3.1.0,>=2.0.0 in c:\users\dell\anaconda3\lib\site-packages (from ipython>=4.0.0->ipywidgets>=7.5.1->giotto-tda) (3.0.20) Requirement already satisfied: pygments in c:\users\dell\anaconda3\lib\site-packages (from ipython>=4.0.0->ipywidgets>=7.5.1->giotto-tda) (2.10.0) Requirement already satisfied: parso<0.9.0,>=0.8.0 in c:\users\dell\anaconda3\lib\site-packages (from jedi>=0.16->ipython>=4.0.0->ipywidgets>=7.5.1->giotto-tda) (0.8.2) Requirement already satisfied: python-dateutil>=2.1 in c:\users\dell\anaconda3\lib\site-packages (from jupyter-client<8.0->ipykernel>=4.5.1->ipywidgets>=7.5.1->giotto-tda) (2.8.2) Requirement already satisfied: pyzmq>=13 in c:\users\dell\anaconda3\lib\site-packages (from jupyter-client<8.0->ipykernel>=4.5.1->ipywidgets>=7.5.1->giotto-tda) (22.2.1) Requirement already satisfied: jupyter-core>=4.6.0 in c:\users\dell\anaconda3\lib\site-packages (from jupyter-client<8.0->ipykernel>=4.5.1->ipywidgets>=7.5.1->giotto-tda) (4.8.1) Requirement already satisfied: pywin32>=1.0 in c:\users\dell\anaconda3\lib\site-packages (from jupyter-core>=4.6.0->jupyter-client<8.0->ipykernel>=4.5.1->ipywidgets>=7.5.1->giotto-tda) (228) Requirement already satisfied: jsonschema!=2.5.0,>=2.4 in c:\users\dell\anaconda3\lib\site-packages (from nbformat>=4.2.0->ipywidgets>=7.5.1->giotto-tda) (3.2.0) Requirement already satisfied: six>=1.11.0 in c:\users\dell\anaconda3\lib\site-packages (from jsonschema!=2.5.0,>=2.4->nbformat>=4.2.0->ipywidgets>=7.5.1->giotto-tda) (1.16.0) Requirement already satisfied: pyrsistent>=0.14.0 in c:\users\dell\anaconda3\lib\site-packages (from jsonschema!=2.5.0,>=2.4->nbformat>=4.2.0->ipywidgets>=7.5.1->giotto-tda) (0.18.0) Requirement already satisfied: attrs>=17.4.0 in c:\users\dell\anaconda3\lib\site-packages (from jsonschema!=2.5.0,>=2.4->nbformat>=4.2.0->ipywidgets>=7.5.1->giotto-tda) (21.2.0) Requirement already satisfied: tenacity>=6.2.0 in c:\users\dell\anaconda3\lib\site-packages (from plotly>=4.8.2->giotto-tda) (8.0.1) Requirement already satisfied: wcwidth in c:\users\dell\anaconda3\lib\site-packages (from prompt-toolkit!=3.0.0,!=3.0.1,<3.1.0,>=2.0.0->ipython>=4.0.0->ipywidgets>=7.5.1->giotto-tda) (0.2.5) Requirement already satisfied: igraph==0.9.9 in c:\users\dell\anaconda3\lib\site-packages (from python-igraph>=0.8.2->giotto-tda) (0.9.9) Requirement already satisfied: texttable>=1.6.2 in c:\users\dell\anaconda3\lib\site-packages (from igraph==0.9.9->python-igraph>=0.8.2->giotto-tda) (1.6.4) Requirement already satisfied: threadpoolctl>=2.0.0 in c:\users\dell\anaconda3\lib\site-packages (from scikit-learn>=0.23.1->giotto-tda) (2.2.0) Requirement already satisfied: notebook>=4.4.1 in c:\users\dell\anaconda3\lib\site-packages (from widgetsnbextension~=3.5.0->ipywidgets>=7.5.1->giotto-tda) (6.4.5) Requirement already satisfied: prometheus-client in c:\users\dell\anaconda3\lib\site-packages (from notebook>=4.4.1->widgetsnbextension~=3.5.0->ipywidgets>=7.5.1->giotto-tda) (0.11.0) Requirement already satisfied: jinja2 in c:\users\dell\anaconda3\lib\site-packages (from notebook>=4.4.1->widgetsnbextension~=3.5.0->ipywidgets>=7.5.1->giotto-tda) (2.11.3) Requirement already satisfied: terminado>=0.8.3 in c:\users\dell\anaconda3\lib\site-packages (from notebook>=4.4.1->widgetsnbextension~=3.5.0->ipywidgets>=7.5.1->giotto-tda) (0.9.4) Requirement already satisfied: argon2-cffi in c:\users\dell\anaconda3\lib\site-packages (from notebook>=4.4.1->widgetsnbextension~=3.5.0->ipywidgets>=7.5.1->giotto-tda) (20.1.0) Requirement already satisfied: nbconvert in c:\users\dell\anaconda3\lib\site-packages (from notebook>=4.4.1->widgetsnbextension~=3.5.0->ipywidgets>=7.5.1->giotto-tda) (6.1.0) Requirement already satisfied: Send2Trash>=1.5.0 in c:\users\dell\anaconda3\lib\site-packages (from notebook>=4.4.1->widgetsnbextension~=3.5.0->ipywidgets>=7.5.1->giotto-tda) (1.8.0) Requirement already satisfied: pywinpty>=0.5 in c:\users\dell\anaconda3\lib\site-packages (from terminado>=0.8.3->notebook>=4.4.1->widgetsnbextension~=3.5.0->ipywidgets>=7.5.1->giotto-tda) (0.5.7) Requirement already satisfied: cffi>=1.0.0 in c:\users\dell\anaconda3\lib\site-packages (from argon2-cffi->notebook>=4.4.1->widgetsnbextension~=3.5.0->ipywidgets>=7.5.1->giotto-tda) (1.14.6) Requirement already satisfied: pycparser in c:\users\dell\anaconda3\lib\site-packages (from cffi>=1.0.0->argon2-cffi->notebook>=4.4.1->widgetsnbextension~=3.5.0->ipywidgets>=7.5.1->giotto-tda) (2.20) Requirement already satisfied: MarkupSafe>=0.23 in c:\users\dell\anaconda3\lib\site-packages (from jinja2->notebook>=4.4.1->widgetsnbextension~=3.5.0->ipywidgets>=7.5.1->giotto-tda) (1.1.1) Requirement already satisfied: testpath in c:\users\dell\anaconda3\lib\site-packages (from nbconvert->notebook>=4.4.1->widgetsnbextension~=3.5.0->ipywidgets>=7.5.1->giotto-tda) (0.5.0) Requirement already satisfied: defusedxml in c:\users\dell\anaconda3\lib\site-packages (from nbconvert->notebook>=4.4.1->widgetsnbextension~=3.5.0->ipywidgets>=7.5.1->giotto-tda) (0.7.1) Requirement already satisfied: pandocfilters>=1.4.1 in c:\users\dell\anaconda3\lib\site-packages (from nbconvert->notebook>=4.4.1->widgetsnbextension~=3.5.0->ipywidgets>=7.5.1->giotto-tda) (1.4.3) Requirement already satisfied: bleach in c:\users\dell\anaconda3\lib\site-packages (from nbconvert->notebook>=4.4.1->widgetsnbextension~=3.5.0->ipywidgets>=7.5.1->giotto-tda) (4.0.0) Requirement already satisfied: mistune<2,>=0.8.1 in c:\users\dell\anaconda3\lib\site-packages (from nbconvert->notebook>=4.4.1->widgetsnbextension~=3.5.0->ipywidgets>=7.5.1->giotto-tda) (0.8.4) Requirement already satisfied: nbclient<0.6.0,>=0.5.0 in c:\users\dell\anaconda3\lib\site-packages (from nbconvert->notebook>=4.4.1->widgetsnbextension~=3.5.0->ipywidgets>=7.5.1->giotto-tda) (0.5.3) Requirement already satisfied: entrypoints>=0.2.2 in c:\users\dell\anaconda3\lib\site-packages (from nbconvert->notebook>=4.4.1->widgetsnbextension~=3.5.0->ipywidgets>=7.5.1->giotto-tda) (0.3) Requirement already satisfied: jupyterlab-pygments in c:\users\dell\anaconda3\lib\site-packages (from nbconvert->notebook>=4.4.1->widgetsnbextension~=3.5.0->ipywidgets>=7.5.1->giotto-tda) (0.1.2) Requirement already satisfied: async-generator in c:\users\dell\anaconda3\lib\site-packages (from nbclient<0.6.0,>=0.5.0->nbconvert->notebook>=4.4.1->widgetsnbextension~=3.5.0->ipywidgets>=7.5.1->giotto-tda) (1.10) Requirement already satisfied: nest-asyncio in c:\users\dell\anaconda3\lib\site-packages (from nbclient<0.6.0,>=0.5.0->nbconvert->notebook>=4.4.1->widgetsnbextension~=3.5.0->ipywidgets>=7.5.1->giotto-tda) (1.5.1) Requirement already satisfied: packaging in c:\users\dell\anaconda3\lib\site-packages (from bleach->nbconvert->notebook>=4.4.1->widgetsnbextension~=3.5.0->ipywidgets>=7.5.1->giotto-tda) (21.3) Requirement already satisfied: webencodings in c:\users\dell\anaconda3\lib\site-packages (from bleach->nbconvert->notebook>=4.4.1->widgetsnbextension~=3.5.0->ipywidgets>=7.5.1->giotto-tda) (0.5.1) Requirement already satisfied: pyparsing!=3.0.5,>=2.0.2 in c:\users\dell\anaconda3\lib\site-packages (from packaging->bleach->nbconvert->notebook>=4.4.1->widgetsnbextension~=3.5.0->ipywidgets>=7.5.1->giotto-tda) (3.0.4) Requirement already satisfied: openml in c:\users\dell\anaconda3\lib\site-packages (0.12.2) Requirement already satisfied: minio in c:\users\dell\anaconda3\lib\site-packages (from openml) (7.1.4) Requirement already satisfied: liac-arff>=2.4.0 in c:\users\dell\anaconda3\lib\site-packages (from openml) (2.5.0) Requirement already satisfied: python-dateutil in c:\users\dell\anaconda3\lib\site-packages (from openml) (2.8.2) Requirement already satisfied: scipy>=0.13.3 in c:\users\dell\anaconda3\lib\site-packages (from openml) (1.7.1) Requirement already satisfied: requests in c:\users\dell\anaconda3\lib\site-packages (from openml) (2.26.0) Requirement already satisfied: xmltodict in c:\users\dell\anaconda3\lib\site-packages (from openml) (0.12.0) Requirement already satisfied: numpy>=1.6.2 in c:\users\dell\anaconda3\lib\site-packages (from openml) (1.21.5) Requirement already satisfied: scikit-learn>=0.18 in c:\users\dell\anaconda3\lib\site-packages (from openml) (0.24.2) Requirement already satisfied: pandas>=1.0.0 in c:\users\dell\anaconda3\lib\site-packages (from openml) (1.3.4) Requirement already satisfied: pyarrow in c:\users\dell\anaconda3\lib\site-packages (from openml) (7.0.0) Requirement already satisfied: pytz>=2017.3 in c:\users\dell\anaconda3\lib\site-packages (from pandas>=1.0.0->openml) (2021.3) Requirement already satisfied: six>=1.5 in c:\users\dell\anaconda3\lib\site-packages (from python-dateutil->openml) (1.16.0) Requirement already satisfied: threadpoolctl>=2.0.0 in c:\users\dell\anaconda3\lib\site-packages (from scikit-learn>=0.18->openml) (2.2.0) Requirement already satisfied: joblib>=0.11 in c:\users\dell\anaconda3\lib\site-packages (from scikit-learn>=0.18->openml) (1.1.0) Requirement already satisfied: urllib3 in c:\users\dell\anaconda3\lib\site-packages (from minio->openml) (1.26.7) Requirement already satisfied: certifi in c:\users\dell\anaconda3\lib\site-packages (from minio->openml) (2021.10.8) Requirement already satisfied: charset-normalizer~=2.0.0 in c:\users\dell\anaconda3\lib\site-packages (from requests->openml) (2.0.4) Requirement already satisfied: idna<4,>=2.5 in c:\users\dell\anaconda3\lib\site-packages (from requests->openml) (2.10) Collecting git+https://github.com/manu-mannattil/nolitsa.git Cloning https://github.com/manu-mannattil/nolitsa.git to c:\users\dell\appdata\local\temp\pip-req-build-q7yz33x9 Resolved https://github.com/manu-mannattil/nolitsa.git to commit 40befcb1ce5535703f90ffe87209181bcdb5eb5c Requirement already satisfied: numpy>=1.11.0 in c:\users\dell\anaconda3\lib\site-packages (from nolitsa==0.1) (1.21.5) Requirement already satisfied: scipy>=0.17.0 in c:\users\dell\anaconda3\lib\site-packages (from nolitsa==0.1) (1.7.1)
Running command git clone -q https://github.com/manu-mannattil/nolitsa.git 'C:\Users\DELL\AppData\Local\Temp\pip-req-build-q7yz33x9'
Requirement already satisfied: plotly in c:\users\dell\anaconda3\lib\site-packages (5.6.0) Requirement already satisfied: tenacity>=6.2.0 in c:\users\dell\anaconda3\lib\site-packages (from plotly) (8.0.1) Requirement already satisfied: six in c:\users\dell\anaconda3\lib\site-packages (from plotly) (1.16.0) Requirement already satisfied: sktime in c:\users\dell\anaconda3\lib\site-packages (0.10.1) Requirement already satisfied: pandas<1.5.0,>=1.1.0 in c:\users\dell\anaconda3\lib\site-packages (from sktime) (1.3.4) Requirement already satisfied: numba>=0.53 in c:\users\dell\anaconda3\lib\site-packages (from sktime) (0.55.1) Requirement already satisfied: numpy<1.22,>=1.21.0 in c:\users\dell\anaconda3\lib\site-packages (from sktime) (1.21.5) Requirement already satisfied: deprecated>=1.2.13 in c:\users\dell\anaconda3\lib\site-packages (from sktime) (1.2.13) Requirement already satisfied: scikit-learn>=0.24.0 in c:\users\dell\anaconda3\lib\site-packages (from sktime) (0.24.2) Requirement already satisfied: scipy<1.8.0 in c:\users\dell\anaconda3\lib\site-packages (from sktime) (1.7.1) Requirement already satisfied: statsmodels>=0.12.1 in c:\users\dell\anaconda3\lib\site-packages (from sktime) (0.13.2) Requirement already satisfied: wrapt<2,>=1.10 in c:\users\dell\anaconda3\lib\site-packages (from deprecated>=1.2.13->sktime) (1.12.1) Requirement already satisfied: llvmlite<0.39,>=0.38.0rc1 in c:\users\dell\anaconda3\lib\site-packages (from numba>=0.53->sktime) (0.38.0) Requirement already satisfied: setuptools in c:\users\dell\anaconda3\lib\site-packages (from numba>=0.53->sktime) (58.0.4) Requirement already satisfied: pytz>=2017.3 in c:\users\dell\anaconda3\lib\site-packages (from pandas<1.5.0,>=1.1.0->sktime) (2021.3) Requirement already satisfied: python-dateutil>=2.7.3 in c:\users\dell\anaconda3\lib\site-packages (from pandas<1.5.0,>=1.1.0->sktime) (2.8.2) Requirement already satisfied: six>=1.5 in c:\users\dell\anaconda3\lib\site-packages (from python-dateutil>=2.7.3->pandas<1.5.0,>=1.1.0->sktime) (1.16.0) Requirement already satisfied: joblib>=0.11 in c:\users\dell\anaconda3\lib\site-packages (from scikit-learn>=0.24.0->sktime) (1.1.0) Requirement already satisfied: threadpoolctl>=2.0.0 in c:\users\dell\anaconda3\lib\site-packages (from scikit-learn>=0.24.0->sktime) (2.2.0) Requirement already satisfied: patsy>=0.5.2 in c:\users\dell\anaconda3\lib\site-packages (from statsmodels>=0.12.1->sktime) (0.5.2) Requirement already satisfied: packaging>=21.3 in c:\users\dell\anaconda3\lib\site-packages (from statsmodels>=0.12.1->sktime) (21.3) Requirement already satisfied: pyparsing!=3.0.5,>=2.0.2 in c:\users\dell\anaconda3\lib\site-packages (from packaging>=21.3->statsmodels>=0.12.1->sktime) (3.0.4)
import numpy as np
import pandas as pd
import matplotlib.pyplot as plt
from nolitsa import dimension, delay
import plotly.express as px
import plotly.graph_objects as go
from sktime.datasets import load_airline, load_shampoo_sales, load_lynx
from sklearn.ensemble import RandomForestRegressor
from sklearn.metrics import r2_score, mean_squared_error
from sklearn.model_selection import train_test_split
from gtda.time_series import SingleTakensEmbedding, takens_embedding_optimal_parameters
from openml.datasets.functions import get_dataset
The function below will generate the embedding for a series given the m and the d parameters. We will then compare it with the implementation from the Giotto-TDA library to see if we implemented it correctly.
def takens(data, m=2, d=1):
emb = np.array([data[0:len(data) - d*m]])
for i in range(1, m):
emb = np.append(emb, [data[i*d:len(data) - d*(m - i)]], axis=0)
return emb.T
t = [i for i in np.arange(0, 20, 0.1)]
y = [np.sin(i) for i in t]
fig = go.Figure()
fig.add_trace(go.Scatter(
x=t, y=y, mode='lines'
))
fig.show()
emb = takens(y)
fig = go.Figure()
fig.add_trace(go.Scatter3d(
x=emb[:, 0], y=emb[:,1], z=[0 for i in range(len(emb[:, 1]))], mode='lines'
))
fig.show()
STE = SingleTakensEmbedding('fixed', 1, 2, n_jobs=-1)
emb = STE.fit_transform(y)
fig = go.Figure()
fig.add_trace(go.Scatter3d(
x=emb[:, 0], y=emb[:,1], z=[0 for i in range(len(emb[:, 1]))], mode='lines'
))
fig.show()
lorenz = get_dataset(42182).get_data(dataset_format='array')[0][0:, 0]
t = [i for i in range(len(lorenz))]
fig = go.Figure()
fig.add_trace(go.Scatter(
x=t, y=lorenz, mode='lines'
))
fig.show()
emb = takens(lorenz, m=3, d=5)
fig = go.Figure()
fig.add_trace(go.Scatter3d( x=emb[:, 0], y=emb[:,1], z=emb[:, 2], mode='lines' ))
fig.show()
STE = SingleTakensEmbedding('fixed', 5, 3, n_jobs=-1)
emb = STE.fit_transform(lorenz)
fig = go.Figure()
fig.add_trace(go.Scatter3d(
x=emb[:, 0], y=emb[:,1], z=emb[:, 2], mode='lines'
))
fig.show()
plt.figure(figsize=(9,4))
plt.xlabel('Time delay (d)')
plt.ylabel('Mutual Information')
plt.plot(delay.dmi(y, maxtau=20))
[<matplotlib.lines.Line2D at 0x2e5f5913fa0>]
def find_optimal_delay(x, maxtau=50):
mi = delay.dmi(x, maxtau=maxtau)
diffmi = np.diff(mi)
return np.where(diffmi > 0)[0][0]
dim = np.arange(1, 10)
f1, f2, f3 = dimension.fnn(y, tau=4, dim=dim)
plt.figure(figsize=(9,4))
plt.xlabel('Dimension (m)')
plt.ylabel('False Nearest Neighbors')
plt.plot(f1)
[<matplotlib.lines.Line2D at 0x2e5f59bb5e0>]
def find_optional_dimension(x, tau, max_dim=10):
dim = np.arange(1, max_dim)
f1, f2, f3 = dimension.fnn(x, tau=tau, dim=dim)
return np.where(f1 < 0.2)[0][0] + 1
def manual_optimal_parameters(x):
d = find_optimal_delay(x)
m = find_optional_dimension(x, tau=d)
print((d, m))
print(takens_embedding_optimal_parameters(y, 50, 10))
manual_optimal_parameters(y)
(16, 7) (4, 2)
print(takens_embedding_optimal_parameters(lorenz, 50, 10))
manual_optimal_parameters(lorenz)
(50, 5) (26, 3)
airline = load_airline()
t = [i for i in range(len(airline))]
fig = go.Figure()
fig.add_trace(go.Scatter(
x=t, y=airline, mode='lines'
))
fig.show()
STE = SingleTakensEmbedding('search', n_jobs=-1)
emb = STE.fit_transform(airline)
print(STE.time_delay_, STE.dimension_)
fig = go.Figure()
fig.add_trace(go.Scatter3d(
x=emb[:, 0], y=emb[:,1], z=emb[:, 2], mode='lines'
))
fig.show()
1 3
shampoo = load_shampoo_sales()
t = [i for i in range(len(shampoo))]
fig = go.Figure()
fig.add_trace(go.Scatter(
x=t, y=shampoo, mode='lines'
))
fig.show()
STE = SingleTakensEmbedding('search', n_jobs=-1)
emb = STE.fit_transform(shampoo)
print(STE.time_delay_, STE.dimension_)
if STE.dimension_ == 2:
z = [0 for i in range(len(emb[:, 0]))]
else:
emb[:, 2]
fig = go.Figure()
fig.add_trace(go.Scatter3d(
x=emb[:, 0], y=emb[:,1], z=z, mode='lines'
))
fig.show()
1 2
lynx = load_lynx()
t = [i for i in range(len(lynx))]
fig = go.Figure()
fig.add_trace(go.Scatter(
x=t, y=lynx, mode='lines'
))
fig.show()
STE = SingleTakensEmbedding('search', n_jobs=-1)
emb = STE.fit_transform(lynx)
print(STE.time_delay_, STE.dimension_)
fig = go.Figure()
fig.add_trace(go.Scatter3d(
x=emb[:, 0], y=emb[:,1], z=emb[:, 2], mode='lines'
))
fig.show()
1 3
rf = RandomForestRegressor()
def forecast_on_phase_space(y, m, d, forecast_horizon):
emb = takens(y, m, d)
# Divide into train and test
X = emb[:, :m-1]
y = emb[:, m-1]
X_train = X[:len(X)-forecast_horizon, :]
y_train = y[:len(y)-forecast_horizon]
X_test = X[len(X)-forecast_horizon:, :]
y_test = y[len(y)-forecast_horizon:]
# Fit the regressor on the training data
rf = RandomForestRegressor()
rf.fit(X_train, y_train)
# Predict the test data
preds = rf.predict(X_test)
print(f'R²: {r2_score(y_test, preds)}')
print(f'RMSE: {mean_squared_error(y_test, preds, squared=False)}')
# Plot the result
preds_ = [np.nan for i in range(len(y)-forecast_horizon)] + list(preds)
t = [i for i in range(len(y))]
fig = go.Figure()
fig.add_trace(go.Scatter(
x=t, y=y, mode='lines'
))
fig.add_trace(go.Scatter(
x=t, y=preds_, mode='lines', line=dict(color='red')))
fig.show()
forecast_on_phase_space(y, 2, 1, 100)
R²: 0.9719899614359981 RMSE: 0.11534528110832125
forecast_on_phase_space(lorenz, 2, 1, 1000)
R²: 0.996758498209725 RMSE: 0.22518926919650034
forecast_on_phase_space(airline, 3, 1, 50)
R²: -0.05326593055369311 RMSE: 83.35677253828871
forecast_on_phase_space(shampoo, 2, 1, 10)
R²: -3.6070309458208873 RMSE: 218.2774351732215
forecast_on_phase_space(lynx, 3, 1, 50)
R²: 0.5993479680299232 RMSE: 1067.8176281800183
# Import libraries
from numpy import linspace,cos,pi,ceil,floor,arange
import matplotlib.pyplot as plt
import seaborn as sns
sns.set(style='whitegrid')
# Sampling a signal bandlimited to 40 Hz
# With sampling rate of 80 Hz
# We are using Nyquist rate
plt.figure(figsize=(18,6))
f = 40
tmin = -0.3
tmax = 0.3
t = linspace(tmin, tmax, 400)
x = cos(2*pi*t) + cos(2*pi*f*t)
plt.plot(t,x,label= "Signal Sampling 40Hz")
plt.legend()
T = 1/80
nmin = ceil(tmin/T)
nmax = floor(tmax/T)
n = arange(nmin, nmax)
x1 = cos(2*pi*n*T) + cos(2*pi*f*n*T)
plt.plot(n*T,x1,"bo",label="Sampling with 80Hz") # "bo" is to explain it with "bullets"
plt.legend()
plt.xlabel("Time")
plt.ylabel("Amplitude")
plt.show()
# Sampling the signal with a sampling rate of 30Hz
# We are "not" using Nyquist rate
T = 1/30
nmin = ceil(tmin/T)
nmax = floor(tmax/T)
n = arange(nmin, nmax)
x2 = cos(2*pi*n*T) + cos(2*pi*f*n*T)
plt.plot(n*T,x2,"-r",markersize = 8,label="Sampling with 30Hz") # "-r" is red colored dashed line"
plt.legend()
plt.xlabel("Time")
plt.ylabel("Amplitude")
plt.show()
# Sampling a signal bandlimited to 40 Hz
# With sampling rate of 80 Hz
# We are using Nyquist rate
plt.figure(figsize=(18,6))
f = 40
tmin = -0.3
tmax = 0.3
t = linspace(tmin, tmax, 400)
x = cos(2*pi*t) + cos(2*pi*f*t)
plt.plot(t,x,label= "Signal Sampling 40Hz")
plt.legend()
T = 1/80
nmin = ceil(tmin/T)
nmax = floor(tmax/T)
n = arange(nmin, nmax)
x1 = cos(2*pi*n*T) + cos(2*pi*f*n*T)
plt.plot(n*T,x1,"bo",label="Sampling with 80Hz") # "bo" is to explain it with "bullets"
# Sampling the signal with a sampling rate of 30Hz
# We are "not" using Nyquist rate
T = 1/30
nmin = ceil(tmin/T)
nmax = floor(tmax/T)
n = arange(nmin, nmax)
x2 = cos(2*pi*n*T) + cos(2*pi*f*n*T)
plt.plot(n*T,x2,"-r",markersize = 8,label="Sampling with 30Hz") # "-r" is red colored dashed line"
plt.legend()
plt.xlabel("Time")
plt.ylabel("Amplitude")
plt.show()